import {defineStore} from 'pinia'
// 导入axios
import request from '@/uitils/request'
import {CategoryItem,ApiRes,TopCategory,SubCategory} from '@/types/data'

import {topCategory} from '@/store/constants'
// 默认数据需要的是对象 需要将数组中数据转换map映射
const defaultCategory=topCategory.map(item=>{
    return {name:item}
})

const  useCategoryStore=defineStore('category',{
    state(){
        return{
            // 所有分类列表
            list:defaultCategory as CategoryItem[],
            // 一级分类
            topCategoryList:{} as TopCategory,
            // 二级分类
            subCategoryList:{} as SubCategory
        }
    },
    getters:{},
    actions:{
        // 获取分类列表
      async  getAllCategory(){
            const res=await request.get<ApiRes<CategoryItem[]>>('/home/category/head')
            this.list=res.data.result
        },
        // 获取一级分类
        async getTopCategory(id:string){
            // const res=await request.get('/category',{params:{id}}) 配置项传参
            const res= await request.get<ApiRes<TopCategory>>(`/category/?id=${id}`)
            this.topCategoryList=res.data.result
        },
        // 二级分类
        async getSubCategory(id:string){
            const res= await request.get<ApiRes<SubCategory>>('/category/sub/filter',{params:{id}}) 
            this.subCategoryList=res.data.result
        }
    }
})
export default useCategoryStore